/**
 * Copyright &copy; com.nmf
 */
package com.nmf.site.biz.applybook.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.nmf.dao.gen.bean.BizApplyBook;
import com.nmf.site.biz.applybook.enums.BookStatusEnums;
import com.nmf.site.biz.applybook.service.BizApplyBookService;
import com.nmf.site.biz.applyperiod.service.BizApplyPeriodService;
import com.nmf.site.common.excep.SiteException;
import com.nmf.site.common.persistence.Page;
import com.nmf.site.common.utils.StringUtils;
import com.nmf.site.common.web.BaseController;

/**
 * 申请书Controller
 * @author bm
 * @version 2019-01-25
 */
@Controller
@RequestMapping(value = "${adminPath}/applybook/bizApplyBook")
public class ProvinceBizApplyBookController extends BaseController {

	@Autowired
	private BizApplyBookService bizApplyBookService;
	@Autowired
	BizApplyPeriodService bizApplyPeriodService;
	@ModelAttribute
	public BizApplyBook get(@RequestParam(required=false) String id,@RequestParam(required=false) String opt,HttpServletRequest request) {
		BizApplyBook entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bizApplyBookService.get(id);
		}
		if (StringUtils.isNotBlank(opt)){
			request.setAttribute("opt", opt);
		}
		if (entity == null){
			entity = new BizApplyBook();
		}
		return entity;
	}
	@RequiresPermissions("applybook:bizApplyBook:view")
	@RequestMapping(value = {"provinceList"})
	public String provinceList(BizApplyBook bizApplyBook, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			Page<BizApplyBook> page = bizApplyBookService.queryByProvince(new Page<BizApplyBook>(request, response), bizApplyBook); 
			model.addAttribute("page", page);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "biz/applybook/province/bizApplyBookList";
		}
		return "biz/applybook/province/bizApplyBookList";
	}
	
	@RequiresPermissions("applybook:bizApplyBook:edit")
	@RequestMapping(value = "accept")
	public String accept(@RequestParam(required=true) String id,@RequestParam(required=true) Integer bookStatus, Model model, RedirectAttributes redirectAttributes) {
		try{
			BookStatusEnums bookStatusEnums = BookStatusEnums.getType(bookStatus);
			BizApplyBook bizApplyBook = bizApplyBookService.get(id);
			BookStatusEnums dbBookStatusEnums = BookStatusEnums.getType(bizApplyBook.getBookStatus());
			
			if(BookStatusEnums.INIT.equals(dbBookStatusEnums)&&BookStatusEnums.accepted.equals(bookStatusEnums)) {

			}else if(BookStatusEnums.accepted.equals(dbBookStatusEnums)&&BookStatusEnums.toApplyBill.equals(bookStatusEnums)) {
				
			}else if(BookStatusEnums.FAILED.equals(bookStatusEnums)) {
				
			}else {
				throw new SiteException("状态错误，请刷新再试");
			}
			BizApplyBook bizApplyBookRecord = new BizApplyBook();
			bizApplyBookRecord.setId(id);
			bizApplyBookRecord.setBookStatus(bookStatus);
			
			bizApplyBookService.save(bizApplyBookRecord);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "biz/applybook/province/bizApplyBookList";
		}
		model.addAttribute("message",  "受理成功");
		return "biz/applybook/province/bizApplyBookList";
	}
	
	
}